#include <vector>
#include <string>
#include <iostream>

using namespace std;


// Leetcode #2255
// 硬解
namespace S1 {
class Solution {
public:
    int countPrefixes(vector<string>& words, string s) {
        // 获取前缀
        vector<string> prefix;
        prefix.reserve(s.size());
        for(int i = 1; i <= s.size(); i ++) {
            prefix.emplace_back(s.substr(0, i));
        }

        int cout = 0;
        for (auto w : words) {
            for (auto p : prefix) {
                if (p == w) {
                    cout ++;
                }
            }
        }
        return cout;
    }
};
}

namespace S2 {
    class Solution {
    public:
        int countPrefixes(vector<string>& words, string s) {
            int cout = 0;
            for (auto w : words) {
                if (w.size() > s.size()) {
                    continue;
                }
                // compare是字符串自带函数 s 的前 w.size() 个字符与 w
                // int compare(size_type pos, size_type len, const string& str) const;    pos起始位置    len 比较长度  str比较对象   0 相等
                if (s.compare(0, w.size(), w) == 0) {
                    cout++;
                }
            }
            return cout;
        }
    };
}

int main() {
    vector<string> workds { "a","b","c","ab","bc","abc"};
    S2::Solution s;
    int res = s.countPrefixes(workds, "abc");
    std::cout << "res:" << res << std::endl;
    return 0;
}